package com.xiaoyang.gatewayserver.filter;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

/**
 * @auther XiaoYang
 * @DATE 2023/5/7-15:14
 */
@Configuration
public class CrossFilter {

    @Bean
    public CorsWebFilter corsWebFilter(){
        CorsConfiguration config = new CorsConfiguration();
        //允许访问所有东西
        //1、配置跨域
        // 允许跨域的头
        config.addAllowedHeader("*");
        // 允许跨域的请求方式
        config.addAllowedMethod("*");
        // 允许跨域的请求来源
        config.addAllowedOrigin("*");
        // 是否允许携带cookie跨域
        config.setAllowCredentials(true);

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        //作用到所有路径上
        source.registerCorsConfiguration("/**",config);
        return new CorsWebFilter(source);
    }

}
